/*
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 *
 * This file is part of retabel.
 *
 * Copyright (c) 2011 by individual contributors as indicated by the
 * @author tags. See the copyright.txt file in the distribution
 * for a full listing of individual contributors.
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this software; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 */

package org.retabel.service.render;

import org.retabel.persistence.entity.document.ContentTypeEnum;
import org.retabel.persistence.entity.document.DocumentConfiguration;
import org.retabel.persistence.entity.report.RenderingEngineEnum;
import org.retabel.service.render.exporter.RenderingEngine;

import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * Feasibility check for a document configuration.
 *
 * <p/>
 * <h3>Extra-Info</h3>
 * Created: 21:29 16.10.11
 *
 * @author Peter Herber
 * @version 1.0
 */
public class DocumentConfigurationValidator {

    private final Set<ContentTypeEnum> contentTypeSet;

    public DocumentConfigurationValidator() {
        contentTypeSet = new HashSet<>();
        contentTypeSet.add(ContentTypeEnum.PDF);
    }

    public boolean testDocumentConfigurationFeasibility(DocumentConfiguration documentConfiguration)
        throws ConfigurationFeasibilityException {
        try {
            // count equal consecutive rendering engines and group them by content type
            RenderingEngineJobSplitter renderingEngineJobSplitter = new RenderingEngineJobSplitter();
            Map<ContentTypeEnum, List<RenderingEngineJob<RenderingEngineEnum>>> splittedDocumentConfiguration =
                renderingEngineJobSplitter.split(documentConfiguration);

            // test feasibility by asking the respective exporters

            for (ContentTypeEnum contentType : splittedDocumentConfiguration.keySet()) {
                List<RenderingEngineJob<RenderingEngineEnum>> renderingEngineJobs = splittedDocumentConfiguration.get(contentType);

                if (renderingEngineJobs != null) {

                    testMixed(contentType, renderingEngineJobs);

                    for (RenderingEngineJob<RenderingEngineEnum> renderingEngineJob : renderingEngineJobs) {
                        testExporterSettings(contentType, renderingEngineJob);
                    }
                }
            }

            // all checks passed
            return true;

        } catch (Exception e) {
            throw new ConfigurationFeasibilityException(e);
        }
    }

    /**
     * Tests the exporter settings. Does the exporter supports the content type? Are concatenations supported?
     *
     * @param contentType the content type.
     * @param renderingEngineJob the job.
     * @throws ConfigurationFeasibilityException if the configuration is not valid.
     */
    private void testExporterSettings(ContentTypeEnum contentType, RenderingEngineJob<RenderingEngineEnum> renderingEngineJob)
        throws ConfigurationFeasibilityException {
        RenderingEngineEnum renderingEngine = renderingEngineJob.getRenderingEngine();

        // the base report design uri isn't required for validation purposes, so we use null
        RenderingEngine exporter = RenderEngineFactory.create(renderingEngine, null);

        if (!exporter.supportsContentType(contentType)) {
            throw new ConfigurationFeasibilityException(renderingEngine + " doesn't support content type '" + contentType + "'");
        }

        if (renderingEngineJob.getEntries().size() > 1) {
            if (!exporter.supportsConcatenationForContentType(contentType)) {
                throw new ConfigurationFeasibilityException(
                    renderingEngine + " doesn't support concatenation for content type '" + contentType + "'");
            }
        }
    }

    /**
     * Tests if mixed configuration between render engines is allowed.
     *
     * @param contentType the content type.
     * @param renderingEngineJobs the render job.
     * @throws ConfigurationFeasibilityException if the mixed configuration is not valid.
     */
    private void testMixed(ContentTypeEnum contentType, List<RenderingEngineJob<RenderingEngineEnum>> renderingEngineJobs)
        throws ConfigurationFeasibilityException {
        if (renderingEngineJobs.size() > 1) {
            // is concatenation of different rendering engine outputs available?

            if (!contentTypeSet.contains(contentType)) {
                throw new ConfigurationFeasibilityException(
                    "concatenation with different rendering engines isn't supported for content type: '" + contentType + "'");
            }
        }
    }



}
